﻿using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity.Approval;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Approval
{
    public class GetReportlistHandler : IRequestHandler<GetReportlistCommand, ApiResult<List<ReportDto>>>
    {
        private readonly IBaseRepository<UsersModel> _userRepository;
        private readonly IBaseRepository<ApprovalModel> _approvalRepository;

        public GetReportlistHandler(IBaseRepository<UsersModel> userRepository, IBaseRepository<ApprovalModel> approvalRepository)
        {
            _userRepository = userRepository;
            _approvalRepository = approvalRepository;
        }
        /// <summary>
        /// 获取报表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<ReportDto>>> Handle(GetReportlistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ReportDto>> result = new ApiResult<List<ReportDto>>();

            var user = _userRepository.GetAll();
            var approval = _approvalRepository.GetAll();

            var report = from u in user
                         join a in approval on u.Id equals a.UserId
                         select new { u.UserName, a.StartTime, a.Hours,a.YeNo};

            if(!string.IsNullOrEmpty(request.UserName))
            {
                report = report.Where(x => x.UserName == request.UserName);
            }
            

            var list = from a in report.Where(x=>x.YeNo==false)
                       group a by a.UserName into form
                       select new ReportDto()
                       {
                           UserName=form.Key,
                           Month1 = form.Where(x=>x.StartTime.Month   == 1 && x.StartTime.Year==2025).Sum(x=>x.Hours),
                           Month2 = form.Where(x => x.StartTime.Month == 2 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month3 = form.Where(x => x.StartTime.Month == 3 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month4 = form.Where(x => x.StartTime.Month == 4 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month5 = form.Where(x => x.StartTime.Month == 5 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month6 = form.Where(x => x.StartTime.Month == 6 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month7 = form.Where(x => x.StartTime.Month == 7 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month8 = form.Where(x => x.StartTime.Month == 8 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month9 = form.Where(x => x.StartTime.Month == 9 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month10 = form.Where(x => x.StartTime.Month == 10 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month11 = form.Where(x => x.StartTime.Month == 11 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                           Month12 = form.Where(x => x.StartTime.Month == 12 && x.StartTime.Year == 2025).Sum(x => x.Hours),
                       };
            result.Code = StatusEnum.OK;
            result.Data = list.ToList();
            result.Message = "获取成功";
            return Task.FromResult(result);

        }
    }
}
